﻿package lz.controls.list
{
    import com.greensock.TweenLite;
    import com.greensock.easing.Quad;
    
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.geom.*;
	/**
	 * Basic List
	 * @author lake
	 * 
	 */	
    public class BasicList
    {
        private var _startPoint	:Point;
        private var _itemLength	:Number;
        private var _itemSpacing	:Number;
        private var _currentPlace	:Number = 0;
        private var _dataIndex		:int;
        private var _slideDirection:int;
        private var _prevDataIndex	:int;
        private var _itemCount		:int;
        private var _isUpdate		:Boolean;
        private var _itemList		:Array;
        private var _dataList		:Array;
		
        public static const HORIZONTAL:int = 0;
        public static const VERTICAL:int = 1;
		/**
		 *view 
		 */		
		private var _listView :Sprite;
		/**
		 *是否增加 
		 */		
		private var isIncrease :Boolean = true;
		/**
		 *标识 向前--1  后退 --  -1
		 */		
		private var preFlag :int = 1;
		/**
		 *是否运动中 
		 */		
		public var isMotioning :Boolean = false;
		/**
		 *持续时间 
		 */		
		public var mDuration :Number = .2;

        public function BasicList(sPoint:Point, 
								  horW:Number, 
								  itemSpace:Number,
								  direct:int = 1)
        {
            _startPoint = sPoint;
            _itemLength = horW;
            _itemSpacing = itemSpace;
            _slideDirection = direct;
            _itemCount = 0;
            _itemList = [];
            _dataList = [];
        }

		public function set currentPlace(value :Number):void
		{
			//trace("currentPlace:",_currentPlace,value);
			if (_currentPlace == value)
			{
				return;
			}
			if(_currentPlace >= value)
			{
				isIncrease = true;
				preFlag = 1;
			}
			else
			{
				var tIndex :int = -_dataIndex;
				if(_slideDirection == HORIZONTAL)
				{
					_itemList[_itemCount-1].x = -(_itemLength + _itemSpacing);
				}
				else
				{
					_itemList[_itemCount-1].y = -(_itemLength + _itemSpacing);
				}
				_itemList[_itemCount-1].readData(_dataList[tIndex - 1]);
				isIncrease = false;
				preFlag = -1;
			}
			_currentPlace = value;
			moveClip();
			//refreshData();
		}
		
		public function set listView(value :Sprite):void
		{
			_listView = value;
			_startPoint.x = _listView.x;
			_startPoint.y = _listView.y;
		}
		
		public function get listView():Sprite
		{
			return _listView;
		}
		
		public function get currentPlace():Number
		{
			return _currentPlace;
		}
		
		public function get itemLength():Number
		{
			return _itemLength;
		}
		
		public function get itemSpacing():Number
		{
			return _itemSpacing;
		}
		
		public function get itemCount():int
		{
			return _itemCount;
		}
		
		public function get dataLength():int
		{
			return _dataList.length;
		}

        public function addItem(item :Object) : void
        {
            _itemList.push(item)
            _itemCount ++;
        }

        public function setData(value:Array) : void
        {
            _dataList = value;
            refreshData(true);
        }
		
		public function update(value :Boolean = true):void
		{
			refreshData(value);
		}

        private function refreshData(value:Boolean = false) : void
        {
            if (!_isUpdate && !value)
            {
                return;
            }
            if (_dataList == null)
            {
                return;
            }
            var i:int = 0;
			var tIndex :int = -_dataIndex;
			//trace("refreshData tIndex = ",tIndex);
            while (i < _itemCount)
            {
                
                if (_dataList[tIndex + i] != null)
                {
                    _itemList[i].readData(_dataList[tIndex + i]);
                }
                else
                {
                    _itemList[i].removeData();
                }
                i++;
            }
        }

        private function moveClip() : void
        {
			TweenLite.killTweensOf(listView,true);
            var tPlace:Number = 0;
            _dataIndex = int(_currentPlace / (_itemLength + _itemSpacing));
			var tItem :Object = _itemList.shift();
			_itemList.push(tItem);
			_isUpdate = true;
            var i:int = 0;
            switch(_slideDirection)
            {
                case HORIZONTAL:
                {
					// 2012-08-06 modify
					listView.x = _startPoint.x;
					isMotioning = true;
					TweenLite.to(listView,mDuration,{ease:Quad.easeOut,x:listView.x - preFlag * (_itemLength + _itemSpacing),onComplete:tHComplete,onCompleteParams:[]});
					function tHComplete():void
					{
						//trace("tHComplete");
						isMotioning = false;
						refreshData();
						listView.x = _startPoint.x;
						i = 0;
						while (i < _itemCount)
						{
							listView.addChild(_itemList[i]);
							_itemList[i].x =  i * (_itemLength + _itemSpacing);
							i ++;
						}
					}
                    break;
                }
                case VERTICAL:
                {
					// 2012-08-06 modify
					listView.y = _startPoint.y;
					isMotioning = true;
					TweenLite.to(listView,mDuration,{ease:Quad.easeOut,y:listView.y - preFlag * (_itemLength + _itemSpacing),onComplete:tVComplete,onCompleteParams:[]});
                    function tVComplete():void
					{
						isMotioning = false;
						refreshData();
						listView.y =  _startPoint.y;;
						i = 0;
						while (i < _itemCount)
						{
							listView.addChild(_itemList[i]);
							_itemList[i].y =  i * (_itemLength + _itemSpacing);
							i ++;
						}
					}
                    break;
                }
            }
        }
		
		public function set dataIndex(value :int):void
		{
			_dataIndex = value;
		}
		
		public function get dataIndex():int
		{
			return _dataIndex
		}
		
		public function clear():void
		{
			_listView = null;
			_itemList = null;
			_dataList = null;
		}
    }
}
